﻿using Autodesk.AutoCAD.DatabaseServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _19直线导圆角功能
{
    public static partial class DatabaseTool
    {
        /// <summary>
        /// 向模型空间添加图形对象
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ent"></param>
        /// <returns></returns>
        public static ObjectId AddEntiy(this Database db, Entity ent)
        {
            ObjectId objId = ObjectId.Null;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                objId = btr.AppendEntity(ent);
                trans.AddNewlyCreatedDBObject(ent, true);
                trans.Commit();
            }
            return objId;
        }
        /// <summary>
        /// 向模型空间添加图形对象
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ents"></param>
        /// <returns></returns>
        public static List<ObjectId> AddEntiy(this Database db, List<Entity> ents)
        {
            List<ObjectId> entIds = new List<ObjectId>();
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                for (int i = 0; i < ents.Count; i++)
                {
                    entIds.Add( btr.AppendEntity(ents[i]));
                    trans.AddNewlyCreatedDBObject(ents[i], true);
                }
                
                trans.Commit();
            }
            return entIds;
        }

        /// <summary>
        /// 删除图形对象
        /// </summary>
        /// <param name="db"></param>
        /// <param name="objId"></param>
        public static void DeleteEntity(this Database db, ObjectId objId)
        {
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {

                Entity ent = (Entity)objId.GetObject(OpenMode.ForWrite);
                ent.Erase();
                trans.Commit();
            }
        }
    }
}
